<template>
  <div class="webview-container">
    <div class="webview-controls">
      <div class="url-input-group">
        <input 
          v-model="url" 
          type="text" 
          placeholder="请输入URL地址" 
          class="url-input"
          @keyup.enter="loadUrl"
        />
        <button @click="loadUrl" class="load-btn">加载</button>
      </div>
      <div class="webview-actions">
        <button @click="goBack" class="action-btn" :disabled="!canGoBack">
          <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
            <polyline points="15 18 9 12 15 6"></polyline>
          </svg>
        </button>
        <button @click="goForward" class="action-btn" :disabled="!canGoForward">
          <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
            <polyline points="9 18 15 12 9 6"></polyline>
          </svg>
        </button>
        <button @click="reload" class="action-btn">
          <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
            <path d="M23 4v6h-6"></path>
            <path d="M1 20v-6h6"></path>
            <path d="M3.51 9a9 9 0 0 1 14.85-3.36L23 10"></path>
            <path d="M20.49 15a9 9 0 0 1-14.85 3.36L1 14"></path>
          </svg>
        </button>
      </div>
    </div>
    
    <div class="webview-content">
      <iframe 
        ref="webviewFrame"
        :src="currentUrl" 
        class="webview-frame"
        @load="onFrameLoad"
        allow="*"
      ></iframe>
    </div>
  </div>
</template>

<script>
export default {
  name: 'WebviewComponent',
  data() {
    return {
      url: '',
      currentUrl: '',
      canGoBack: false,
      canGoForward: false,
      history: [],
      currentHistoryIndex: -1
    }
  },
  methods: {
    loadUrl() {
      if (this.url) {
        // 确保URL以http://或https://开头
        let formattedUrl = this.url;
        if (!formattedUrl.startsWith('http://') && !formattedUrl.startsWith('https://')) {
          formattedUrl = 'https://' + formattedUrl;
        }
        this.currentUrl = formattedUrl;
        
        // 添加到历史记录
        this.history = this.history.slice(0, this.currentHistoryIndex + 1);
        this.history.push(formattedUrl);
        this.currentHistoryIndex = this.history.length - 1;
        
        this.updateNavigationState();
      }
    },
    
    goBack() {
      if (this.currentHistoryIndex > 0) {
        this.currentHistoryIndex--;
        this.currentUrl = this.history[this.currentHistoryIndex];
        this.url = this.currentUrl;
        this.updateNavigationState();
      }
    },
    
    goForward() {
      if (this.currentHistoryIndex < this.history.length - 1) {
        this.currentHistoryIndex++;
        this.currentUrl = this.history[this.currentHistoryIndex];
        this.url = this.currentUrl;
        this.updateNavigationState();
      }
    },
    
    reload() {
      const iframe = this.$refs.webviewFrame;
      if (iframe) {
        iframe.contentWindow.location.reload();
      }
    },
    
    onFrameLoad() {
      this.updateNavigationState();
    },
    
    updateNavigationState() {
      this.canGoBack = this.currentHistoryIndex > 0;
      this.canGoForward = this.currentHistoryIndex < this.history.length - 1;
    }
  }
}
</script>

<style scoped>
.webview-container {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.webview-controls {
  padding: 16px;
  background: #f8fafc;
  border-bottom: 1px solid #e2e8f0;
  display: flex;
  align-items: center;
  gap: 16px;
}

.url-input-group {
  display: flex;
  flex: 1;
  max-width: 600px;
}

.url-input {
  flex: 1;
  padding: 8px 12px;
  border: 1px solid #d1d5db;
  border-radius: 4px 0 0 4px;
  font-size: 14px;
  outline: none;
}

.url-input:focus {
  border-color: #3b82f6;
}

.load-btn {
  padding: 8px 16px;
  background: #3b82f6;
  color: white;
  border: none;
  border-radius: 0 4px 4px 0;
  cursor: pointer;
  font-size: 14px;
}

.load-btn:hover {
  background: #2563eb;
}

.webview-actions {
  display: flex;
  gap: 8px;
}

.action-btn {
  padding: 8px;
  border: 1px solid #d1d5db;
  background: white;
  border-radius: 4px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.action-btn:hover:not(:disabled) {
  background: #f3f4f6;
}

.action-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.action-btn svg {
  width: 16px;
  height: 16px;
}

.webview-content {
  flex: 1;
  position: relative;
}

.webview-frame {
  width: 100%;
  height: 100%;
  border: none;
  background: white;
}
</style>